package net.mysoft.framework.util;

import java.util.ArrayList;
import java.util.List;

import net.mysoft.framework.model.TreeNode;

import com.sun.jmx.snmp.Timestamp;

/**
 * 
 * @author liuyang
 *
 */
public class TreeUtil {

	/**
	 * 递归生成树  效率较慢 不适合层级过多
	 * @param listNode
	 * @param childNodeList
	 * @param parentId
	 * @return
	 */
	public static List<TreeNode> generateTree(List<TreeNode> listNode,List<TreeNode> childNodeList,String parentId){
		if(childNodeList==null)
			childNodeList = new ArrayList();
		for(TreeNode node : listNode){
			if(node.getParentId().equals(parentId)){
				List<TreeNode> childChildNodeList = TreeUtil.generateTree(listNode,node.getChildren(),node.getId());
				node.setChildren(childChildNodeList);
				childNodeList.add(node);		
			}
		}
		return childNodeList;
	}

	public static void main(String[] args){
		Timestamp date = new Timestamp();
		
	
		Long l = date.getDateTime();
	
		//System.out.println("开始生成");
		List<TreeNode> listNode = new ArrayList<TreeNode>();
		
		for(int i=1;i<20000;i++){
			TreeNode tn = new TreeNode();
			tn.setId(i+"");
			if(i<10){
				tn.setParentId("0");
			}else{
				tn.setParentId(i%10+1+"");
			}
			listNode.add(tn);
		}
		TreeUtil.generateTree(listNode,new ArrayList(),"0");
		Timestamp date2 = new Timestamp();
		Long l2 = date2.getDateTime();
		//System.out.println("结束生成"+(l-l2));
	}
}
